package com.web.dao.impl;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

@Service(value="SequenceDao")
@Scope(value = "singleton")
public class SequenceDao {
	
	@Resource(name="jdbcTemplate")
	private JdbcTemplate jdbcTemplate;
	
	public void createSeq(String name) {
		String sql = "insert into sequence(name, nextValue) values(?, ?);";
		jdbcTemplate.update(sql, new Object[]{name.toLowerCase()});
	}
	
	public int getNextSeq(String name) {
		this.updateByName(name);
		String sql = "select nextvalue from sequence where name=?;";
		int nextValue = jdbcTemplate.queryForInt(sql, new Object[]{name.toLowerCase()});
		return nextValue;
	}
	
	public void updateByName(String name) {
		String sql = "update sequence set nextvalue = nextvalue + 1 where name = ?;";
		jdbcTemplate.update(sql, new Object[]{name});
	}

}
